#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('xref-streams');

my $n_tests = 5;

# Handle xref stream with more entries than reported (bug 2872265)
$td->runtest("xref with short size",
             {$td->COMMAND => "qpdf --show-xref xref-with-short-size.pdf"},
             {$td->FILE => "xref-with-short-size.out",
              $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("recover xref with short size",
             {$td->COMMAND => "qpdf xref-with-short-size.pdf a.pdf"},
             {$td->FILE => "xref-with-short-size-recover.out",
              $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("show new xref stream",
             {$td->COMMAND => "qpdf --show-xref a.pdf",
                  $td->FILTER => "perl filter-xref-offsets.pl"},
             {$td->FILE => "xref-with-short-size-new.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
# Handle appended file that reuses the xref stream object number
$td->runtest("override xref stream - xref",
             {$td->COMMAND => "qpdf --show-xref reuse-xref-stream.pdf"},
             {$td->FILE => "reuse-xref-stream-xref.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("override xref stream - object",
             {$td->COMMAND => "qpdf --show-object=9 reuse-xref-stream.pdf"},
             {$td->FILE => "reuse-xref-stream-obj9.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

cleanup();
$td->report($n_tests);
